import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;


public class UserInputGUI extends JPanel implements ActionListener{
private JButton applyButton;
private JPanel outer;
private JLabel userInput;
private JPanel inputPanel;
private JPanel buttonPanel;
private JTextField userInputText;
private JFrame inputFrame;
private ViewsSelect viewsSelected;
private LinkedList files;
private String queue;
private String st;
private ConfigurationSingleton config;

  public UserInputGUI(String queue, ViewsSelect viewsSelected){
  this.queue = queue;
  this.viewsSelected = viewsSelected;
  }

  public UserInputGUI(String queue, ViewsSelect viewsSelected, LinkedList files){
     this.queue = queue;
     this.viewsSelected = viewsSelected;
     this.files = files;
  }

  public UserInputGUI(String queue, LinkedList files){
     this.queue = queue;
     this.files = files;
  }

  public UserInputGUI(String queue, ConfigurationSingleton config){
       this.queue = queue;
       this.config = config;
  }

  public void run(){

  outer = new JPanel();

  JPanel inputPanel = new JPanel();
  inputPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

  userInput = new JLabel(queue+":");
  inputPanel.add(userInput);

  userInputText = new javax.swing.JTextField(10);
  inputPanel.add(userInputText);

  buttonPanel = new JPanel();
  buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

  applyButton = new JButton("Apply");
  applyButton.addActionListener(this);
  inputPanel.add(applyButton);

  outer.setLayout(new BorderLayout());
  outer.add(inputPanel, BorderLayout.CENTER);
  outer.add(buttonPanel, BorderLayout.SOUTH);

  inputFrame = new JFrame(queue);
  inputFrame.setSize(300, 100);
  inputFrame.getContentPane().add(outer);
  inputFrame.setLocation(450,10);
  inputFrame.show();
  }

  private String returnInput(){
     String str = userInputText.getText();
     return str;
  }

public void actionPerformed(ActionEvent e) {
  Object source = e.getSource();
    if(source == applyButton){
        st = userInputText.getText();
        if(queue == "Add View"){
         addView();
        }
        else if(queue =="Add Files To View"){
         addFilesToView();
        }
        else if(queue == "Add Key Word"){
              keyWord();
        }
        else if(queue == "Delete Key Word"){
              keyWord();
        }
        else if(queue == "Set Scan Interval"){
              setScanInterval();
        }

        inputFrame.hide();
     }
  }

public void setScanInterval(){

   try{
      Integer integer = Integer.decode(st);
      int interval = integer.intValue()*1000;
      config.setUpdateInterval(interval);
   }
   catch(NumberFormatException e){
      inputFrame.hide();
      JOptionPane.showMessageDialog(new JFrame(),
      "You must enter a number of seconds in the form of an integer.",
      "Error",
      JOptionPane.ERROR_MESSAGE);
   }
}

public void addView(){
   if(viewsSelected.alreadyExists(st)){
      inputFrame.hide();
      JOptionPane.showMessageDialog(new JFrame(),
      "The View "+st+" already exists.",
      "Error",
      JOptionPane.ERROR_MESSAGE);
   }
   else{
      Views newView = new Views(st);
      viewsSelected.addView(st, newView);
   }
}


public void addFilesToView(){
   String viewName = returnInput();
   if(!viewsSelected.alreadyExists(viewName)){
         inputFrame.hide();
         JOptionPane.showMessageDialog(new JFrame(),
         "The View "+viewName+" does not exist.",
         "Error",
         JOptionPane.ERROR_MESSAGE);
   }
   else{
      Views temp = viewsSelected.nameToView(viewName);
      temp.addFiles(files);
      temp.printFiles();
   }
}

/*
public void deleteFilesToView(){
      String viewName = returnInput();
      //error check if exists:
      Views temp = viewsSelected.nameToView(viewName);
      temp.deleteFile(files);
      temp.printFiles();
}
*/

public void keyWord(){
   String keyWord = returnInput();
   Iterator fileIter = files.listIterator();
   while(fileIter.hasNext()){
      DBFile temp = (DBFile)fileIter.next();
      if(queue == "Add Key Word"){
         temp.appendToKeyWords(keyWord);
      }
      else{
         temp.deleteFromKeyWords(keyWord);
      }
   }
}


}